/**
*      author: Y_Yao
*      created: 2024-09-12 22:27:29
**/

#include<iostream>
using namespace std;
using LL = long long;

LL a[100010];
int p;

LL pow(LL x, int n, int p)
{
    LL ret = 1;
    while (n)
    {
        if (n & 1) ret = ret * x % p;
        x = x * x % p;
        n = n >> 1;
    }
    return ret;
}

LL C(LL n, LL m)
{
    if (m > n) return 0;
    return ((a[n] * pow(a[m], p - 2, p)) % p * pow(a[n - m], p - 2, p) % p);
}

int Lucas(LL a, LL b, int p)
{
    if (b == 0) return 1;
    return C(a % p, b % p) * Lucas(a / p, b / p, p) % p;
}

void solve()
{
    int n, m;
    cin >> n >> m >> p;
    a[0] = 1;
    for (int i = 1;i <= p;i++) a[i] = a[i - 1] * i % p;
    cout << Lucas(n + m, n, p) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}